export default {
    namespace: true,
    state: {
        menuList: [
            {index: '1', path: 'Home', label: '首页', icon: 'fa-home'},
            {
                index: '2', path: 'InstitutionsManage', label: '机构管理', icon: 'fa-group',
                children: [
                    {index: '2-1', path: 'InfoManage', label: '信息管理', icon: 'fa-angle-right'},
                    {index: '2-2', path: 'BusinessManage', label: '商机管理', icon: 'fa-angle-right'},
                    {index: '2-3', path: 'ProductionManage', label: '产品管理', icon: 'fa-angle-right'},
                    {index: '2-4', path: 'ContractManage', label: '合同管理', icon: 'fa-angle-right'},
                    {index: '2-5', path: 'LoggerManage', label: '日志管理', icon: 'fa-angle-right'},
                    {index: '2-6', path: 'BookingManage', label: '预约管理', icon: 'fa-angle-right'},
                    {index: '2-7', path: 'AfterSalesLogger', label: '售后日志', icon: 'fa-angle-right'},
                    {index: '2-8', path: 'TaskList', label: '任务清单', icon: 'fa-angle-right'},
                ]
            },
            {index: '3', path: 'EmployeeManage', label: '员工管理', icon: 'fa-user'},
            {index: '4', path: 'LibManage', label: '资料库管理', icon: 'fa-folder-open'},
            {index: '5', path: 'SystemManage', label: '系统对接管理', icon: 'fa-gears'},
            {index: '6', path: 'StaticAnalyse', label: '统计分析', icon: 'fa-bar-chart'},
            {index: '7', path: 'UserManage', label: '用户管理', icon: 'fa-users'},
            {index: '8', path: 'SystemConfig', label: '系统配置', icon: 'fa-cog'},
            {index: '9', path: 'SystemLogger', label: '系统日志', icon: 'fa-edit'},
            {index: '10', path: 'SafeExit', label: '安全退出', icon: 'fa-sign-out'}
        ],
        sysInfoActive: false,
        navHasActive: false
    },
    mutations: {
        /**
         * 设置所有菜单状态，默认都不选中
         * @param state
         * @param data
         */
        setMenuFalse(state, data) {
            let {vm, status = false} = data;
            _.forEach(state.menuList, (item) => {
                !item.children ? vm.$set(item, 'active', status) : _.forEach(item.children, (subItem) => {
                    vm.$set(subItem, 'active', status)
                });
            });
        },
        /**
         * 设置[系统信息]按钮状态，默认不选中
         * @param state
         * @param data
         */
        setSysInfoStatus(state, data) {
            let {status = false} = data;
            state.sysInfoActive = status;
        },
        /**
         * 更新菜单
         * @param state
         * @param data
         */
        changeMenu(state, data) {
            let {vm, curMenu = {}, isSub, subIndex} = data;
            vm.$store.commit('setMenuFalse', _.assign(data, {status: false}));
            vm.$store.commit('setSysInfoStatus', {status: false});
            if (isSub) {
                subIndex = subIndex || 0;
                vm.$set(curMenu.children[subIndex], 'active', true);
            } else {
                vm.$set(curMenu, 'active', true);
            }
        },
        /**
         * 初始化菜单属性
         * @param state
         * @param data
         */
        initMenu(state, data) {
            let {vm, index, attr, flag} = data;
            let list = vm.$store.state.menuStore.menuList;
            if (_.includes(index, '-')) {
                // 二级菜单遍历
                _.forEach(list, menu => {
                    if (menu.children) {
                        vm.$store.commit('forEachMenu', {
                            vm,
                            index,
                            attr,
                            flag,
                            list: menu.children
                        })
                    }
                })
            } else {
                // 一级菜单遍历
                vm.$store.commit('forEachMenu', {vm, index, attr, flag, list})
            }
            if (vm.$store.state.menuStore.navHasActive) {
                vm.$store.commit('setSysInfoStatus', {status: false});
            } else {
                vm.$store.state.menuStore.navHasActive = false;
                vm.$store.commit('setSysInfoStatus', {status: true});
            }
        },
        /**
         * 菜单列表遍历，默认只遍历一级，设置当前菜单选项，返回成功与否
         * @param state
         * @param data
         * @return {boolean}
         */
        forEachMenu(state, data) {
            let {vm, index, attr, flag, list} = data;
            for (let i = 0, len = list.length; i < len; i++) {
                if (list[i].index === index) {
                    vm.$set(list[i], attr, flag);
                    vm.$store.state.menuStore.navHasActive = true;
                }
            }
        }
    },
    actions: {}
}